//
//  AppDelegate.m
//  weibo-OC
//
//  Created by diyuyma on 15/12/10.
//  Copyright © 2015年 zhangkaiyun. All rights reserved.
//

#import "AppDelegate.h"
#import "MainViewController.h"
#import "WelComeViewController.h"
#import "NewFeaturViewController.h"
#import "UserAccountViewModel.h"
#import "ComposeViewController.h"
#import "PictureSelectorViewController.h"

@interface AppDelegate ()

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    
    self.window = [[UIWindow alloc] initWithFrame: [UIScreen mainScreen].bounds];
    self.window.backgroundColor = [UIColor whiteColor];
    self.window.rootViewController = [self defaultRootController];
    // 设置主体颜色
    [self setThemeColor];
    
    // 注册通知
    [self registerNotification];
    [self.window makeKeyAndVisible];
    
    return YES;
}

// MARK: 监听通知
- (void)registerNotification {
    // object  观测某个对象发出的通知 如果为nil  就是观测所有的对象发出的通知
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(switchToRootVC:) name:AppSwitchRootViewControler object:nil];
}

- (void)switchToRootVC: (NSNotification *)n {
    // 跳转到 MainVC
    if ([n.object isKindOfClass:[NSString class]] != false) {
        self.window.rootViewController = [[WelComeViewController alloc] init];
        return ;
    }
     self.window.rootViewController = [[MainViewController alloc] init];
}

- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

// MARK: 判断用户是否是登陆 并且是否是更新版本
- (UIViewController *)defaultRootController {
    
    if ([[UserAccountViewModel alloc] init].userLogin) {
        return [self isNewVersion] ? [[NewFeaturViewController alloc] init] : [[WelComeViewController alloc] init];
    }
    
    return [[MainViewController alloc] init];
}

- (BOOL)isNewVersion {
    // 比较新旧版本
    // 1.获取新版本
    NSString *versionStr = [NSBundle mainBundle].infoDictionary[@"CFBundleShortVersionString"];
    double versionNum = [versionStr doubleValue];
    
    // 2.获取 旧版本 一般存放在沙盒3
    NSString *key = @"sandBoxKey";
    NSUserDefaults *userdefaults = [NSUserDefaults standardUserDefaults];
    double lastVersion = [userdefaults doubleForKey:key];
    
    // 3.保存新版本
    [userdefaults setDouble:versionNum forKey:key];
    // 同步沙盒
    [userdefaults synchronize];
    
    // 比较新旧版本
    return versionNum > lastVersion;
}

- (void)setThemeColor {
    
    [UINavigationBar appearance].tintColor = themeColor;
    [UITabBar appearance].tintColor = themeColor;
}

- (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

- (void)applicationDidEnterBackground:(UIApplication *)application {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}

- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

@end
